package com.example.android.ui.system

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.liveData
import androidx.lifecycle.viewModelScope
import com.example.android.data.api.HomeService
import com.example.android.data.bean.SystemList
import com.example.android.http.Result
import com.example.android.http.requestFlow
import com.zxf.basic.base.BaseViewModel
import com.zxf.basic.utils.ToastUtils
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import javax.inject.Inject

@ExperimentalCoroutinesApi
@HiltViewModel
class SystemViewModel @Inject constructor(private val repository: SystemRepository, val app: Application) : BaseViewModel() {
    private val service = getService(HomeService::class.java)
    private val failLive = MutableLiveData<Any>()

    fun getSystemData(): LiveData<List<SystemList>?> {
        val liveData = MutableLiveData<List<SystemList>?>()
        viewModelScope.launch {
            repository.getSystem()
                .onStart {
                    ToastUtils.show("体系请求开始")
                }.collect { result ->
                    when (result) {
                        is Result.Success -> liveData.value = result.data
                        else -> liveData.value = null
                    }
                }
        }
        return liveData
    }

    fun getSystem() = liveData {
        withContext(Dispatchers.IO) {
            withTimeout(10000) {
                val data = service.getSystem().data
                emit(data)
            }
        }
    }

    /**
     * 不使用repository，也挺简洁
     */
    fun getSystemInfoData(): LiveData<List<SystemList>> {
        val liveData = MutableLiveData<List<SystemList>>()
        viewModelScope.launch {
            requestFlow(
                api = {
                    service.getSystem()
                },
                onStart = {
                    ToastUtils.show("体系开始请求")
                },
                onComplete = {
                    ToastUtils.show("体系结束请求")
                }
            ).collect {
                liveData.value = it
            }
        }
        return liveData
    }

    fun getSystemInfo(): LiveData<List<SystemList>> {
        val liveData = MutableLiveData<List<SystemList>>()
        viewModelScope.launch {
            repository.getSystemData()
                .collect {
                    when (it) {
                        is Result.Success -> liveData.value = it.data as List<SystemList>
                        is Result.Error -> failLive.value = it.code
                        else -> {

                        }
                    }

                }
        }
        return liveData
    }
}